/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.LoaiNguoiDungEnum;
import dao.CuaHangDAO;
import dao.LoaiNguoiDungDAO;
import dao.NguoiDungDAO;
import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.CuaHang;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;

/**
 *
 * @author 0
 */
public class NhanVienGUI extends javax.swing.JDialog {

    /**
     * Creates new form NhanVienGUI
     */
    private JErrorProvider errorProvider;
    private MyEventListener ButtonLuuClickedListener;
    private static Logger logger = Logger.getLogger(NhanVienGUI.class);
    private NguoiDung nguoiDung;
    private NguoiDung oldNguoiDung;

    public NhanVienGUI(java.awt.Frame parent, boolean modal, NguoiDung nguoiDung) throws Exception {
        super(parent, modal);
        initComponents();
        errorProvider = new JErrorProvider();
        SetTabOrder();
        FormatControls();
        AutoLoad();
        this.nguoiDung = nguoiDung;
        oldNguoiDung = new NguoiDung();
        oldNguoiDung.setCmnd(nguoiDung.getCmnd());
        oldNguoiDung.setCuaHang(nguoiDung.getCuaHang());
        oldNguoiDung.setDiaChi(nguoiDung.getDiaChi());
        oldNguoiDung.setDienThoai(nguoiDung.getDienThoai());
        oldNguoiDung.setIsDelete(false);
        oldNguoiDung.setLoaiNguoiDung(nguoiDung.getLoaiNguoiDung());
        oldNguoiDung.setNgaySinh(nguoiDung.getNgaySinh());
        oldNguoiDung.setTenNguoiDung(nguoiDung.getTenNguoiDung());
        if (nguoiDung.getId() == 0) {
            setTitle("Thêm nhân viên");
        } else {
            setTitle("Sửa nhân viên");
            SetDataToConTrol();
            if(nguoiDung.getLoaiNguoiDung().getId()==LoaiNguoiDungEnum.Admin.getCode()){
                cboLoaiNguoiDung.setEnabled(false);
            }
        }
    }

    private void SetTabOrder() {
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy = new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtTenNhanVien, txtCMND, txtDienThoai, txtDiaChi, dtNgaySinh, cboLoaiNguoiDung, cboCuaHang, btnLuu, btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }

    private void FormatControls() {
        Utility.FormatNumberStringTextfield(txtCMND);
        Utility.FormatNumberStringTextfield(txtDienThoai);
    }

    private void AutoLoad() throws Exception {
        List<LoaiNguoiDung> loaiNguoiDungs = (List<LoaiNguoiDung>) LoaiNguoiDungDAO.LayDanhSach(LoaiNguoiDung.class, MainGUI.nguoiDung);
        loaiNguoiDungs.remove(0);
        loaiNguoiDungs.remove(loaiNguoiDungs.size() - 1);
        Collections.sort(loaiNguoiDungs, new Comparator<LoaiNguoiDung>() {
            @Override
            public int compare(LoaiNguoiDung o1, LoaiNguoiDung o2) {
                return o1.getTenLoaiNguoiDung().compareTo(o2.getTenLoaiNguoiDung());
            }
        });
        List<CuaHang> cuaHangs = (List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class, MainGUI.nguoiDung);
        Collections.sort(cuaHangs, new Comparator<CuaHang>() {
            @Override
            public int compare(CuaHang o1, CuaHang o2) {
                return o1.getTenCuaHang().compareTo(o2.getTenCuaHang());
            }
        });
        Utility.SetDataSourceInCombo(loaiNguoiDungs, cboLoaiNguoiDung);
        Utility.SetDataSourceInCombo(cuaHangs, cboCuaHang);

        cboCuaHang.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof CuaHang) {
                    CuaHang cuaHang = (CuaHang) value;
                    setText(cuaHang.getTenCuaHang());
                }
                return this;
            }
        });
        cboCuaHang.setSelectedIndex(-1);

        cboLoaiNguoiDung.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof LoaiNguoiDung) {
                    LoaiNguoiDung loaiNguoiDung = (LoaiNguoiDung) value;
                    setText(loaiNguoiDung.getTenLoaiNguoiDung());
                }
                return this;
            }
        });
        cboLoaiNguoiDung.setSelectedIndex(-1);
    }

    private int CheckDataBeforeLuu() {
        int dem = 0;
        errorProvider.clear();
        if (txtTenNhanVien.getText().trim().isEmpty()) {
            errorProvider.setError(txtTenNhanVien, "required");
            dem++;
        }
        if (txtCMND.getText().isEmpty()) {
            errorProvider.setError(txtCMND, "required");
            dem++;
        }
        if (txtDiaChi.getText().trim().isEmpty()) {
            errorProvider.setError(txtDiaChi, "required");
            dem++;
        }
        if (dtNgaySinh.getDate() == null) {
            errorProvider.setError(dtNgaySinh, "required");
            dem++;
        }
        if (cboCuaHang.getModel().getSelectedItem() == null) {
            errorProvider.setError(cboCuaHang, "required");
            dem++;
        }
        if (cboLoaiNguoiDung.getModel().getSelectedItem() == null) {
            errorProvider.setError(cboLoaiNguoiDung, "required");
            dem++;
        }
        return dem;
    }

    private void SetDataToConTrol() {
        txtTenNhanVien.setText(nguoiDung.getTenNguoiDung());
        txtDiaChi.setText(nguoiDung.getDiaChi());
        txtDienThoai.setText(nguoiDung.getDienThoai());
        txtCMND.setText(nguoiDung.getCmnd());
        dtNgaySinh.setDate(nguoiDung.getNgaySinh());
        cboCuaHang.getModel().setSelectedItem(nguoiDung.getCuaHang());
        cboLoaiNguoiDung.getModel().setSelectedItem(nguoiDung.getLoaiNguoiDung());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtTenNhanVien = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDienThoai = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dtNgaySinh = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        cboLoaiNguoiDung = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cboCuaHang = new javax.swing.JComboBox();
        btnLuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nhân viên");
        setResizable(false);

        jLabel1.setText("Tên nhân viên");

        jLabel2.setText("CMND");

        jLabel3.setText("Điện thoại");

        jLabel4.setText("Địa chỉ");

        jLabel5.setText("Ngày sinh");

        jLabel6.setText("Loại người dùng");

        jLabel7.setText("Cửa hàng");

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDienThoai)
                            .addComponent(txtCMND)
                            .addComponent(txtTenNhanVien)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
                        .addGap(43, 43, 43))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnLuu)
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dtNgaySinh, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(cboLoaiNguoiDung, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cboCuaHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(btnThoat))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(dtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(cboLoaiNguoiDung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(cboCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLuu)
                    .addComponent(btnThoat))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        try {
            // TODO add your handling code here:
            if (CheckDataBeforeLuu() > 0) {
                return;
            }
            CuaHang cuaHang = (CuaHang) cboCuaHang.getModel().getSelectedItem();
            LoaiNguoiDung loaiNguoiDung = (LoaiNguoiDung) cboLoaiNguoiDung.getModel().getSelectedItem();
            nguoiDung.setCmnd(txtCMND.getText());
            nguoiDung.setCuaHang(cuaHang);
            nguoiDung.setDiaChi(txtDiaChi.getText().trim());
            nguoiDung.setDienThoai(txtDienThoai.getText());
            nguoiDung.setIsDelete(false);
            nguoiDung.setLoaiNguoiDung(loaiNguoiDung);
            nguoiDung.setNgaySinh(dtNgaySinh.getDate());
            nguoiDung.setTenNguoiDung(txtTenNhanVien.getText().trim());
            if (nguoiDung.getId() == 0) {
                if (ButtonLuuClickedListener == null) {
                    return;
                }
                int id = NguoiDungDAO.Them(nguoiDung);
                nguoiDung.setId(id);
                JOptionPane.showMessageDialog(null, "Thêm thành công");
                ButtonLuuClickedListener.myEventOccured(nguoiDung);
            } else {
                NguoiDungDAO.Sua(nguoiDung);
                JOptionPane.showMessageDialog(this, "Cập nhật thành công");
            }
            dispose();
        } catch (Exception ex) {
            nguoiDung.setCmnd(oldNguoiDung.getCmnd());
            nguoiDung.setCuaHang(oldNguoiDung.getCuaHang());
            nguoiDung.setDiaChi(oldNguoiDung.getDiaChi());
            nguoiDung.setDienThoai(oldNguoiDung.getDienThoai());
            nguoiDung.setIsDelete(false);
            nguoiDung.setLoaiNguoiDung(oldNguoiDung.getLoaiNguoiDung());
            nguoiDung.setNgaySinh(oldNguoiDung.getNgaySinh());
            nguoiDung.setTenNguoiDung(oldNguoiDung.getTenNguoiDung());
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnLuuActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnThoat;
    private javax.swing.JComboBox cboCuaHang;
    private javax.swing.JComboBox cboLoaiNguoiDung;
    private org.jdesktop.swingx.JXDatePicker dtNgaySinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtTenNhanVien;
    // End of variables declaration//GEN-END:variables

    /**
     * @param ButtonLuuClickedListener the ButtonLuuClickedListener to set
     */
    public void setButtonLuuClickedListener(MyEventListener ButtonLuuClickedListener) {
        this.ButtonLuuClickedListener = ButtonLuuClickedListener;
    }
}
